package com.appclub.kitsilano.info;

import java.io.IOException;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Window;
import com.appclub.kitsilano.R;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

@SuppressLint({ "NewApi", "NewApi", "NewApi", "NewApi", "NewApi", "NewApi",
		"NewApi", "NewApi", "NewApi", "NewApi", "NewApi", "NewApi", "NewApi",
		"WorldReadableFiles", "WorldReadableFiles" })
public class ContactUs extends SherlockActivity {

	private ProgressDialog ProgressDialog1 = null;
	private Button send;
	private EditText editText1;
	private EditText editText2;
	private static final String SAVE_INFORMATION = "save_information";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.contact_us);
		setSupportProgressBarIndeterminateVisibility(false);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);

		editText1 = (EditText) findViewById(R.id.editText1);
		editText2 = (EditText) findViewById(R.id.editText2);

		send = (Button) findViewById(R.id.send);
		send.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				try {
					SendMail();

					Toast.makeText(getApplicationContext(), "sent",
							Toast.LENGTH_SHORT).show();

					Intent intent = new Intent(ContactUs.this, Info.class);
					startActivity(intent);
					overridePendingTransition(R.anim.bottom_in,
							R.anim.bottom_out);
				} catch (MessagingException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
	}

	public void SendMail() throws MessagingException, IOException {
		new Thread() {
			@SuppressWarnings("unused")
			public void run() {
				SharedPreferences pre = getSharedPreferences(SAVE_INFORMATION,
						MODE_WORLD_READABLE);
				String content = pre.getString("save", "");
				String[] Information = content.split(";");
				Properties props = new Properties();
				props.put("mail.smtp.host", "smtp.163.com");
				props.put("mail.smtp.auth", "true");
				Session session = Session.getInstance(props);
				session.setDebug(true);
				MimeMessage message = new MimeMessage(session);
				try {
					Address fromAddress = null;
					fromAddress = new InternetAddress("contactappclub@163.com");
					message.setFrom(fromAddress);

					Address toAddress = null;
					toAddress = new InternetAddress("kitsappclub@gmail.com");
					message.addRecipient(Message.RecipientType.TO, toAddress);

					message.setSubject("An E-mail From A User");
					message.setText(editText2.getText().toString() + "\n"
							+ "My E-mail Adress: "
							+ editText1.getText().toString());
					message.saveChanges();

					Transport transport = null;
					transport = session.getTransport("smtp");
					transport.connect("smtp.163.com", "contactappclub@163.com",
							"appclub");
					transport.sendMessage(message, message.getAllRecipients());
					transport.close();
					ProgressDialog1.dismiss();
				} catch (Exception e) {
				}
			}
		}.start();

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			Intent intent = new Intent(this, Info.class);
			startActivity(intent);
			overridePendingTransition(R.anim.right_in, R.anim.right_out);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Intent intent = new Intent(this, Info.class);
			startActivity(intent);
			overridePendingTransition(R.anim.right_in, R.anim.right_out);
			this.finish();
		}
		return true;
	}
}